package com.lmh.ymw.controller.product;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lmh.ymw.entity.ProductCategory;
import com.lmh.ymw.entity.RespBean;
import com.lmh.ymw.service.product.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("ProductCategory")
@CrossOrigin
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService service;

    @GetMapping("/")
    public RespBean listAll(){
       // List<ProductCategory> all = service.findAll(p);

        List<ProductCategory> caidan = service.caidan();

        return RespBean.ok("查询成功",caidan);
    }


    @GetMapping("/list")
    public RespBean list(ProductCategory p/*,@RequestParam Integer pageNum*/){
        /*PageHelper.startPage(pageNum,3);*/
        System.out.println(p.getId());
        List<ProductCategory> list = service.queryProductCategorylist(p);
        /*PageInfo<ProductCategory> list = new PageInfo<>(productCategories);*/
        if (list != null) {
            return RespBean.ok("查询成功",list);
        }else {
            return RespBean.ok("查询失败");
        }
    }

    @GetMapping("list/{id}")
    public RespBean getId(@PathVariable Integer id){
        ProductCategory byId = service.getById(id);
        if (byId != null) {
            return RespBean.ok("查询成功",byId);

        }else {
            return RespBean.ok("查询失败");
        }
    }
    @PostMapping("add")
    public RespBean add(@RequestParam ProductCategory p){
        int i = service.addProductCategory(p);
        if (i != 0) {
            return RespBean.ok("修改成功",i);
        }else {
            return RespBean.error("修改失败");
        }
    }

    @DeleteMapping("/{id}")
    public RespBean DelId(@PathVariable Integer id){
        int byId = service.deleteById(id);
        if (byId != 0) {
            return RespBean.ok("删除成功",byId);

        }else {
            return RespBean.ok("删除失败");
        }
    }
    @PutMapping("/")
    public RespBean up(ProductCategory p){
        int i = service.modifyProductCategory(p);
        if (i != 0) {
            return RespBean.ok("修改成功",p);
        }else {
            return RespBean.error("修改失败");
        }

    }
}
